<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Sample</title>
<meta name="viewport"
            content="width=device-width, initial-scale=1.0">
<meta name="HandheldFriendly" content="true">
</head>
<body>
<canvas id="myCanvas" width="300" height="150"
        style="border:1px dashed #c3c3c3;">
   这里放置的内容用于浏览器不支持Canvas的情况
</canvas>
<script>
var canvas =document.getElementById("myCanvas");
if (canvas && canvas.getContext) {
   	var cxt = canvas.getContext("2d");
   
    // 创建ImageData对象
	var imgd = cxt.createImageData(50,50);
	var pix = imgd.data;
	
	// 遍历每个像素和设置透明度
	var n = pix.length;
	for (var i = 0; i < n; i += 4) {
	  pix[i  ] = 255; // 红色通道
	  pix[i+3] = 127; // alpha通道
	}
	
	// 画ImageData对象
	cxt.putImageData(imgd, 0,0);
}
</script>
</body>
</html>
